package office.action.sale;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import office.BusiUtil;
import office.action.sale.form.CustomerConditionForm;
import office.action.sale.form.CustomerForm;
import office.action.sale.form.CustomerInfoForm;
import office.action.sale.wrapper.CustomerWrapper;
import office.bo.sale.impl.CustomerProp;
import office.service.sale.ICustomerPropService;
import office.service.sale.ICustomerService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import platform.logging.LogContext;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;

/**
 * 
 * 标书管理
 * 
 * @author ZhangYQ
 * 
 */
public class CustomerAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private ICustomerService customerService;
	private Page page; // 分页信息
	private List<CustomerWrapper> wrapperList;
	private CustomerForm form;
	private CustomerConditionForm condition;
	private String id;
	private List<User> userList =new ArrayList<User>() ;
	@Resource
	private UserManagerService userManagerService;
	@Resource
	private ICustomerPropService customerPropService;
	private Logger customerLog = Logger.getLogger("customer");
	private List<CustomerProp> cpList;
	
	private List<CustomerInfoForm> ciList;
	
	private Map<String ,String> ciMap = new HashMap<String,String>();
	
	private Map<String ,String> ciidMap = new HashMap<String,String>();

	/**
	 * 获取所有的客户以及根据条件进行查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findAllCustomer() throws Exception {
		String resultString="listCustomerByPage";
		if(condition==null){
			condition=new CustomerConditionForm();
			resultString="findAllCustomer";
		}
		if(id!=null){
			condition.setWriteMan(id);
		}else{
			condition.setWriteMan((String)getUserMap().get("userId"));
		}
		page = customerService.getPageInfo(condition);
		
		wrapperList = customerService.findAllCustomer(condition,page);
		
		if(BusiUtil.validateRole(getUserRoleName())){
			userList = userManagerService.getUsersByAllDepartment(getUserDepartmentId());
		}
		return resultString;
	}

	/**
	 * 初始化编辑客户页面
	 * @return
	 * @throws Exception
	 */
	public String gotoEditCustomer() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = customerService.getById(id);
			for (Iterator<CustomerInfoForm> iterator = form.getSetInfo().iterator(); iterator.hasNext();) {
				CustomerInfoForm customerInfoForm = (CustomerInfoForm) iterator.next();
				ciMap.put(customerInfoForm.getCustomerPropId(), customerInfoForm.getContent());
				ciidMap.put(customerInfoForm.getCustomerPropId(), customerInfoForm.getId());
			}
		}
		cpList = customerPropService.getAllCustomerProp();
		//获取销售人员接口待实现
		userList=userManagerService.getUserByRoleAndCompany("xsry",getUserCompanyId());
		
		return "gotoEditCustomer";
	}
	
	public String showCustomer() throws Exception {
		if(null!=id&&!"".equals(id)){
			form = customerService.getById(id);
		}
		return "showCustomer";
	}
	/**
	 * 编辑客户
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editCustomer() throws Exception {
		try {
			String msg="编辑成功";
			if (null == form.getId()|| ("").equals(form.getId())) {
				msg="新建成功";
				form.setWriteMan(getUserMap().get("userId").toString());
				form.setCreateDate(new Date());
			}
			form.setUpdateDate(new Date());
			if(ciList != null)
			form.setSetInfo(new HashSet<CustomerInfoForm>(ciList));
			customerService.editCustomer(form);
			LogContext.info(customerLog,"editcustomer",getUserCname(),form.getName(),"success");
			this.getAjaxMessagesJson().setMessage(true, "", msg);
		} catch (Exception e) {
			LogContext.info(customerLog,"editcustomer",getUserCname(),form.getName(),"failure");
			this.handleException(e);
		}
		return RESULT_AJAXJSON;
	}

	/**
	 * 
	 * <br/>
	 * 方法描述:分页条件查询产品结构
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listCustomerByPage() throws Exception {
		try {
			if(condition.getWriteMan()==null){
				condition.setWriteMan((String)getUserMap().get("userId"));
			}
			wrapperList = customerService.findAllCustomer(condition, page);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return "listCustomerByPage";
	}

	/**
	 * 初始化产品结构列表操作菜单
	 * 
	 * @return
	 * @throws Exception
	 */

	public String gotoCustomerMenu() throws Exception {
		return "gotoCustomerMenu";
	}

	/**
	 * 跳转到客户删除确认页面
	 */
	public String gotoDeleteCustomer(){
		return "gotoDeleteCustomer";
	}
	
	/**
	 * 删除客户
	 * @return
	 * @throws Exception
	 */
	public String delCustomer() throws Exception {
		CustomerForm customerForm = customerService.getById(id);
		try {
			customerService.delCustomerById(id);
			LogContext.info(customerLog,"editcustomer",getUserCname(),customerForm.getName(),"success");
			this.getAjaxMessagesJson().setMessage(true, "", "操作成功");
		} catch (Exception e) {
			LogContext.info(customerLog,"editcustomer",getUserCname(),customerForm.getName(),"failure");
			log.error(e.getMessage(), e);
			throw e;
		}
		return RESULT_AJAXJSON;
	}

	public ICustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<CustomerWrapper> getWrapperList() {
		return wrapperList;
	}

	public void setWrapperList(List<CustomerWrapper> wrapperList) {
		this.wrapperList = wrapperList;
	}

	public CustomerForm getForm() {
		return form;
	}

	public void setForm(CustomerForm form) {
		this.form = form;
	}

	public CustomerConditionForm getCondition() {
		return condition;
	}

	public void setCondition(CustomerConditionForm condition) {
		this.condition = condition;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public ICustomerPropService getCustomerPropService() {
		return customerPropService;
	}

	public void setCustomerPropService(ICustomerPropService customerPropService) {
		this.customerPropService = customerPropService;
	}

	public List<CustomerProp> getCpList() {
		return cpList;
	}

	public void setCpList(List<CustomerProp> cpList) {
		this.cpList = cpList;
	}

	public List<CustomerInfoForm> getCiList() {
		return ciList;
	}

	public void setCiList(List<CustomerInfoForm> ciList) {
		this.ciList = ciList;
	}

	public Map<String, String> getCiMap() {
		return ciMap;
	}

	public void setCiMap(Map<String, String> ciMap) {
		this.ciMap = ciMap;
	}

	public Map<String, String> getCiidMap() {
		return ciidMap;
	}

	public void setCiidMap(Map<String, String> ciidMap) {
		this.ciidMap = ciidMap;
	}
	
}
